//
//  YLWelcomeViewController.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/4.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit
import SDWebImage

//欢迎界面
class YLWelcomeViewController: UIViewController {
    
    //懒加载
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var userImageView : UIImageView  = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let imageUrl = YLUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large{
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        imageView.layer.cornerRadius = 45
        
        imageView.layer.masksToBounds = true
        
        return imageView
        
        
    }()
    
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        label.alpha = 0
        if let name = YLUserAccountViewModel.sharedUserAccount.userAccount?.name{
            label.text = "欢迎回来,\(name)"
        } else {
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFont(ofSize: 13)
        
        label.textColor = UIColor.darkGray
        
        
        return label
    }()
    
    
    
    //加载view
    override func loadView() {
        view = backgroundImageView
    }
    //view读取完毕
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI ()
        
        
    }

    
    private func setupUI () {
        
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(300)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(userImageView)
        }
        
    }
    
    private func startAnimation() {
        
        userImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animate(withDuration: 1, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }, completion: {(_) -> Void in
            
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                //切换到主页
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootVCNotificationName), object: nil)
            })
            
        })
        
        
    }
    
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }


}
